﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Configuration;
using Security;


namespace IPR_TrainExamSys_TY
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //获取欲启动进程名
            string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            ////获取版本号 
            //CommonData.VersionNumber = Application.ProductVersion; 
            //检查进程是否已经启动，已经启动则显示报错信息退出程序。 
            if (System.Diagnostics.Process.GetProcessesByName(strProcessName).Length > 1)
            {

                MessageBox.Show("系统程序已经运行！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Application.Exit();
                return;
            }

            try
            {
                bool bIsReg = false;
                string sRegCode = ConfigurationManager.ConnectionStrings["ConStringEncrypt"].ConnectionString;
                ConnectionStringSettingsCollection cssc = new ConnectionStringSettingsCollection();
                string sMechinaCode = Security.Security.GetCpuID() + Security.Security.GetDiskID();
                string sRegResult = checkzcm(sMechinaCode, sRegCode, ref bIsReg);
                if (!bIsReg)
                {
                    FrmRegister fr = new FrmRegister(sRegResult, bIsReg, sMechinaCode, sRegCode);
                    fr.ShowDialog();
                    fr.Close();
                }
                else
                {
                    string sTitle = ConfigurationManager.ConnectionStrings["Title"].ConnectionString;
                    BaseClass.CLoginInfo.Title = sTitle;
                    FrmLoginNew fl = new FrmLoginNew();
                    fl.ShowDialog();
                    fl.Close();
                    switch (fl.frmLoginResult)
                    {
                        case DialogResult.Yes:
                            Application.Run(new FrmMain(fl.m_userinfo));
                            break;

                        case DialogResult.No:
                            Application.Exit();
                            break;
                    }


                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "错误");
            }
        }


        static string checkzcm(string sMechinaCode, string sRegCode, ref bool bIsReg)
        {
            string sRegCryp = "";
            string sRegDate = "";
            string sRegResult = "";
            if (sRegCode == "")
            {
                sRegResult = "请先注册系统！请联系开发商注册!";
                bIsReg = false;
            }
            else
            {
                sRegCryp = Security.Security.DecryptDES(sRegCode, "ZZIPRYFB");
                sRegDate = sRegCryp.Substring(sRegCryp.Length - 8, 8);
                if (sRegCryp.Substring(0, sRegCryp.Length - 8) != sMechinaCode)
                {
                    sRegResult = "注册码不正确！请联系开发商注册！";
                    bIsReg = false;
                }
                else
                {
                    DateTime dtCur = DateTime.Now;
                    DateTime dtRegDate = new DateTime(Int16.Parse(sRegDate.Substring(0, 4)), Int16.Parse(sRegDate.Substring(4, 2)), Int16.Parse(sRegDate.Substring(6, 2)));

                    if (dtCur.Subtract(dtRegDate).Days > 0)
                    {
                        sRegResult = "授权注册码已过期！请联系开发商注册！";
                        bIsReg = false;
                    }
                    else
                    {
                        sRegResult = "已注册！";
                        bIsReg = true;
                    }
                }
            }
            return sRegResult;
        }


    }
}
